const {
    PlanType
} = require('../../../model/plan/planType');

function getdate(times) {
    var now = new Date(times),
        y = now.getFullYear(),
        m = now.getMonth() + 1,
        d = now.getDate();
    return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
}


module.exports = async (req, res) => {
    // 获取post参数
    let pageSize = req.fields.pageSize
    let currentPage = req.fields.current

    let data = []
    if (!req.fields.data) {
        data = await PlanType.find().sort('-createTime').skip((currentPage - 1) * pageSize).limit(pageSize);
    } else {
        let { name } = req.fields.data
        data = await PlanType.find({
            name: new RegExp(name, 'i'),
        }).sort('-createTime').skip((currentPage - 1) * pageSize).limit(pageSize);

    }
    let totalCount = data.length
    let list = data.map((item) => {
        return Object.assign({}, { 'id': item._id, 'name': item.name, 'createTime': getdate(item.createTime) })
    })
    res.send({ 'code': 0, "data": list, 'total': totalCount });
}